package com.chinanet.repository.faultanticipate;


import com.chinanet.repository.ExtendedQuerydslJpaRepository;
import com.chinanet.entry.faultanticipate.Detail;
import com.chinanet.entry.faultanticipate.QDetail;
import com.querydsl.core.types.dsl.StringPath;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RestResource;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public interface DetailRepository extends PagingAndSortingRepository<Detail, Long>, QuerydslBinderCustomizer<QDetail>, QuerydslPredicateExecutor<Detail>,ExtendedQuerydslJpaRepository<Detail,Long> {
    Detail findByName(@Param("name") String name);

    @RestResource(path = "keyword", rel = "keyword")
    @Query(value = "select * from detail d left join detail_keywords d_k on d_k.details_id=d.id left join keyword k on d_k.keywords_id = k.id  where k.id=:keywordId", nativeQuery = true)
    Page<Detail> getAllByVersion(@Param("keywordId") long keywordId, Pageable pageable);

    @RestResource(path = "notKeyword", rel = "not/keyword")
    @Query(value = "select * from detail d left join detail_keywords d_k on d_k.details_id=d.id left join keyword k on d_k.keywords_id = k.id where k.id<>:keywordId or k.id is null", nativeQuery = true)
    Page<Detail> getAllNotByVersion(@Param("keywordId") long keywordId, Pageable pageable);

    @RestResource(path = "relation", rel = "relation")
    @Query(value = "select * from detail d left join detail_relations d_r on d_r.details_id=d.id left join relation r on d_r.relations_id = r.id where r.id=:relationId", nativeQuery = true)
    Page<Detail> getAllByRelation(@Param("relationId") long relationId, Pageable pageable);

    @RestResource(path = "notRelation", rel = "not/relation")
    @Query(value = "select * from detail d left join detail_relations d_r on d_r.details_id=d.id left join relation r on d_r.relations_id = r.id  where  r.id<>:relationId or r.id is null", nativeQuery = true)
    Page<Detail> getAllNotByRelation(@Param("relationId") long relationId, Pageable pageable);

    @RestResource(path = "team", rel = "team")
    @Query(value = "select * from detail d left join detail_teams d_t on d_t.details_id=d.id left join team t on d_t.teams_id = t.id where t.id=:teamId", nativeQuery = true)
    Page<Detail> getAllByTeam(@Param("teamId") long teamId, Pageable pageable);

    @RestResource(path = "notTeam", rel = "not/team")
    @Query(value = "select * from detail d left join detail_teams d_t on d_t.details_id=d.id left join team t on d_t.teams_id = t.id  where  d_t.details_id not in (select d_t.details_id from detail_teams d_t where d_t.teams_id =:teamId )  or t.id is null group by d.id", nativeQuery = true)
    Page<Detail> getAllNotByTeam(@Param("teamId") long teamId, Pageable pageable);

    @RestResource(path = "role", rel = "role")
    @Query(value = "select * from detail d join detail_teams d_t on d.id = d_t.details_id join team t on t.id=d_t.teams_id join rolle_teams r_t on r_t.teams_id=t.id join rolle r on r.id = r_t.rolles_id where r.id =:roleId group by d.id", nativeQuery = true)
    Page<Detail> getAllByRole(@Param("roleId") long roleId, Pageable pageable);

    @Query(value = "select * from detail d join detail_teams d_t on d.id = d_t.details_id join team t on t.id=d_t.teams_id join rolle_teams r_t on r_t.teams_id=t.id join rolle r on r.id = r_t.rolles_id where r.id =:roleId and :param like concat('%',:value,'%') group by d.id", nativeQuery = true)
    Page<Detail> getAllByRole(@Param("param") String param,@Param("value") String value,@Param("roleId") long roleId, Pageable pageable);
    // 过滤
    default void customize(QuerydslBindings bindings, QDetail detail) {
/*        bindings.bind(relation.na
me).first((path, value) -> path.contains(value));
        bindings.bind(relation.name).first((path, value) -> path.containsIgnoreCase(value));*/
        bindings.bind(String.class).first((StringPath path, String value) -> {
            // 中文乱码解决问题
            try {
                String newValue = URLDecoder.decode(value, "UTF-8");
                System.out.println(path);
                System.out.println(newValue);
                return path.contains(newValue);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            }
        });
    }

/*    List<Detail> findAll(Specification<Detail> specification);*/

    //select * from detail d left join detail_teams d_t on d_t.details_id=d.id left join team t on d_t.teams_id = t.id where t.id=:teamId
/*    default Page<Detail> findAll(Predicate predicate, Pageable pageable) {
        System.out.println("进行数据关联");
        return null;
    }*/
}